<?php

// +----------------------------------------------------------------------
// | 用户
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\api\v3;

use app\model\ucenter\Label as LabelModel;
use app\model\ucenter\LabelGroup as LabelGroupModel;
use app\pay\event\UserBalance as UserBalanceEvent;
use app\pay\event\UserBalanceCash as UserBalanceCashEvent;
use wechat\mini_program\Code as CodeMini;
use app\model\ucenter\UserMoneyLog as UserMoneyLogModel;
use app\model\admin\FeedBack as FeedbackModel;
use app\model\ucenter\MemberInfo as MemberInfoModel;
use app\model\pay\Pay as PayModel;

class Vip extends Common
{

    // 首页
    public function index()
    {

        if ($this->user) {

//            $user['payPwd']  = $user['payPwd']?1:0;
            $info['all_profit'] = UserMoneyLogModel::where('money', '>', 0)
                ->where('user_id', $this->user['id'])
                ->where('type', 'in', [11, 12, 13, 14, 15, 16, 18, 20, 21, 66])->sum('money');
//            $user['profit'] =sprintf('%.2f', $user['profit']);
//            //推荐人
//            $user['recommend'] = $user['pid']?Db::name('user')->where('id',$user['pid'])->value('nickname'):'无';
//            //实名
//            $real_name = Db::name('user_authentication')->where('uid',$this->auth->id)->find();
//            $user['real_name'] = $real_name?$real_name['name']:'';
//            $user['id_card'] = $real_name?$real_name['idNumber']:'';

            return $this->successAjax($info);
        } else {
            return $this->errorAjax('用户未登录');
        }

    }


    public function pay()
    {
        $insert = [];
        if ($this->is_examine) {
            $vip = MemberInfoModel::where('id', '>', 4)->where('type', $this->request->param('vip_id'))->find();
            $title = '开通F2读者';
            $insert['type'] = 'vip';
        } else {
            $vip = MemberInfoModel::where('id', '<', 5)->where('type', $this->request->param('vip_id'))->find();
            $title = '押金升级中级合伙人';
            $insert['type'] = 'deposit';
        }
        $payment_code = $this->request->param('payment_code', 'alipay');
        $insert['user_id'] = $this->user['id'];
        $insert['payment_code'] = $payment_code;
        $insert['api_pay_status'] = '';
        $insert['total_amount'] = $vip['money'];
        $insert['title'] = $title;
        $pay = PayModel::create($insert);
        if ($pay) {
            if (isset($this->param['is_h5']) && 1 == $this->param['is_h5']) {
                $data['webview'] = 0;
                $data['info'] = alipay_h5_pay($pay['pay_sn'], $pay['total_amount'], $pay['title'], $pay['type']);
            } else {
                $data['webview'] = 0;
                $data['url'] = (string)url('/api/notify/alipay/h5', ['pay_sn' => $pay['pay_sn']], true, true);
                if ($insert['payment_code'] == 'wxpay') {
                    $data['info'] = wechat_app_pay($pay);
                } else {
                    $data['info'] = alipay_app_pay($pay['pay_sn'], $pay['total_amount'], $pay['title'], $pay['type']);
                }
                $data['payment'] = $payment_code;
            }
            return $this->successAjax($data);
        } else {
            return $this->successAjax('系统故障，请稍候再试');
        }
    }

}
